package es.capgemini.extjsplugin.ui;

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import es.capgemini.extjsplugin.Activator;

public class AttributesTableLabelProvider extends LabelProvider implements
		ITableLabelProvider {

	// Names of images used to represent checkboxes
	public static final String CHECKED_IMAGE 	= "checked";
	public static final String UNCHECKED_IMAGE  = "unchecked";

	// For the checkbox images
	private static ImageRegistry imageRegistry = new ImageRegistry();

	/**
	 * Note: An image registry owns all of the image objects registered with it,
	 * and automatically disposes of them the SWT Display is disposed.
	 */ 
	static {
		String iconPath = "icons/"; 
		imageRegistry.put(CHECKED_IMAGE, Activator.getImageDescriptor(iconPath + CHECKED_IMAGE + ".gif"));
		imageRegistry.put(UNCHECKED_IMAGE, Activator.getImageDescriptor(iconPath + UNCHECKED_IMAGE + ".gif"));
	}
	
	/**
	 * Returns the image with the given key, or <code>null</code> if not found.
	 */
	private Image getImage(boolean isSelected) {
		String key = isSelected ? CHECKED_IMAGE : UNCHECKED_IMAGE;
		return  imageRegistry.get(key);
	}
	/**
	 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
	 */
	public Image getColumnImage(Object element, int columnIndex) {
		if(columnIndex == 0){
			boolean isSelected=((ExtendedField) element).getIncluded();
			return getImage(isSelected);
		}
		return null;
	}

	@Override
	public String getColumnText(Object obj, int columnIndex) {
		ExtendedField field=(ExtendedField)obj;
		switch(columnIndex){
		case 0:
			//return String.valueOf(field.getIncluded());
			return "";
			
		case 1:
			return field.getField().getName();
			
		case 2:
			return field.getField().getType().getSimpleName();
		default:
			throw new RuntimeException("Should not happen");
			
		}
	}

}
